Skip to content

Fix encoding of a "%" next to a valid sequence or at the end - #40

Merged
blakeembrey merged 2 commits into
pillarjs:masterfrom
yxshee:fix/invalid-percent-before-escape
Nov 11, 2026
Merged

blakeembrey merged 2 commits into
pillarjs:masterfrom
yxshee:fix/invalid-percent-before-escape

Conversation

@yxshee

@yxshee yxshee commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

An invalid % is matched together with the character after it (%[0-9A-Fa-f]?[^0-9A-Fa-f]). That has two effects:

  1. If the next character is the % of a valid sequence, that sequence is encoded too:
    encodeUrl("/%%41");        // "/%25%2541", expected "/%25%41"
    encodeUrl("/%2%20snow");   // "/%252%2520snow", expected "/%252%20snow"
  2. A truncated sequence at the end of the string is not matched at all, so it is left invalid:
    encodeUrl("/%A");  // "/%A", expected "/%25A"
    encodeUrl("/a%2"); // "/a%2", expected "/a%252"

Both differ from what the README promises (% is only encoded when it is not part of a valid sequence). 2.x has the same behavior, so this affects res.location() / res.redirect() in Express too.

The fix replaces the consuming match with a lookahead, %(?![0-9A-Fa-f]{2}), so only the % itself is encoded and the following characters are handled on their own.

Testing: two new specs fail on master and pass with the fix; npm test (prettier, tsc, vitest, 100% coverage) passes. I also ran 300k random strings built from %, hex digits, other ASCII, non-ASCII and lone surrogates: on master 23,403 outputs still contained a % not followed by two hex digits, with the fix none did, and every output contains only URL code points.

yxshee and others added 2 commits November 9, 2026 02:23
An invalid "%" consumed the following character, so "%%41" became
"%25%2541" (the valid %41 was encoded too) and a truncated "%A" at
the end was left as-is. Use a lookahead instead.
@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (6545e6a) to head (1d79e99).

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #40   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines            5         5           
  Branches         1         1           
=========================================
  Hits             5         5           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@blakeembrey

Copy link
Copy Markdown
Member

Made some additional changes to handle large inputs properly but otherwise LGTM.

@blakeembrey
blakeembrey merged commit 6d39100 into pillarjs:master Nov 11, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants